/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import bean.Aula;
import bean.Curso;
import bean.Horarios;
import controlador.ControladorCadastraHorario;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Júnior
 */
public class PainelHorarioTurno extends javax.swing.JPanel {

    /**
     * Creates new form PainelHorarioTurno
     */
    List<Curso> listaCursos;
    List<String> listaTurmas;
    List<DefaultTableModel> t = new ArrayList();
    List<Horarios> listaHorarios;
    List<Aula> listaAulas;
    /*DefaultTableModel tabelaHorarioTurno = new DefaultTableModel(new String[]{"Início", "Fim", "Segunda", "Terça", "Quarta", "Quinta", "Sexta"}, 0) {
     @Override
     public boolean isCellEditable(int row, int column) {
     return false;
     }
     };*/

    public PainelHorarioTurno() {
        initComponents();
        listaCurso();
       
        if (jComboBoxCurso.getItemCount() > 0) {

            listaTurma();

        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jComboBoxCurso = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jComboBoxTurno = new javax.swing.JComboBox();
        jPanelBotoes = new javax.swing.JPanel();
        jButtonGerarPdf = new javax.swing.JButton();

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Relatório por Turno");

        jComboBoxCurso.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jComboBoxCurso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxCursoActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel3.setText("Curso :");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel4.setText("Turno :");

        jComboBoxTurno.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jComboBoxTurno.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Manhã", "Tarde", "Noite" }));
        jComboBoxTurno.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxTurnoActionPerformed(evt);
            }
        });

        jPanelBotoes.setLayout(new java.awt.GridBagLayout());

        jButtonGerarPdf.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jButtonGerarPdf.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/List.png"))); // NOI18N
        jButtonGerarPdf.setText("Gerar PDF");
        jButtonGerarPdf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGerarPdfActionPerformed(evt);
            }
        });
        jPanelBotoes.add(jButtonGerarPdf, new java.awt.GridBagConstraints());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 830, Short.MAX_VALUE)
            .addComponent(jPanelBotoes, javax.swing.GroupLayout.DEFAULT_SIZE, 830, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBoxTurno, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBoxCurso, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(596, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jComboBoxCurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jComboBoxTurno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 330, Short.MAX_VALUE)
                .addComponent(jPanelBotoes, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBoxCursoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxCursoActionPerformed
        listaTurma();
       
    }//GEN-LAST:event_jComboBoxCursoActionPerformed

    private void jButtonGerarPdfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGerarPdfActionPerformed
    	if(listaCursos.isEmpty()){
        	JOptionPane.showMessageDialog(null, "Não há Cursos Cadastrados");
        }else if (listaTurmas.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Não há Turmas cadastradas para o curso selecionado");
        } else {
        int opcao2 = listaTurmas.size();

        String turno;
        int opcao3 = jComboBoxTurno.getSelectedIndex();
        if (opcao3 == 0) {
            turno = "Manhã";
        } else if (opcao3 == 1) {
            turno = "Tarde";
        } else {
            turno = "Noite";
        }

        if (opcao2 == 0) {
            JOptionPane.showMessageDialog(null, "Não há Disciplina(s) cadastrada(s) para o Curso selecionado.");
        } else {

            for (int i = 0; i < opcao2; i++) {
                buscar(i);

            }

            Pdf pdf = new Pdf();

            int opcao = jComboBoxCurso.getSelectedIndex();

            try {
                pdf.relatorioTurno(listaCursos.get(opcao).getNome(), listaTurmas, turno, t);
               
            } catch (IOException ex) {
                Logger.getLogger(PainelHorarioTurma.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        }

    }//GEN-LAST:event_jButtonGerarPdfActionPerformed

    private void buscar(int i) {

        String turno;
        int opcao = jComboBoxCurso.getSelectedIndex();

        int opcao3 = jComboBoxTurno.getSelectedIndex();
        if (opcao3 == 0) {
            turno = "Manhã";
        } else if (opcao3 == 1) {
            turno = "Tarde";
        } else {
            turno = "Noite";
        }

        Curso curso = listaCursos.get(opcao);
        ControladorCadastraHorario ch = new ControladorCadastraHorario();
        listaAulas = ch.listaAula(curso.getCodCurso(), turno.substring(0, 1), listaTurmas.get(i));

        t.add(i, montaTabela());
        for (int j = 0; j < listaAulas.size(); j++) {
            listaAulas.remove(j);
        }
    }

    private void jComboBoxTurnoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxTurnoActionPerformed
       
        listaTurma();

    }//GEN-LAST:event_jComboBoxTurnoActionPerformed

    private void listaTurma() {
        int opcao = jComboBoxCurso.getSelectedIndex();
        if (listaCursos.isEmpty()) {
            JOptionPane.showMessageDialog(null, "Não há Cursos Cadastrados");
        } else {
            Curso curso = listaCursos.get(opcao);
            ControladorCadastraHorario ch = new ControladorCadastraHorario();
            listaTurmas = ch.listaTurma(curso.getCodCurso());

        }
    }

    private void preencheTabela(String turno) {

        /*tabelaHorarioTurno.setNumRows(0);
         ControladorCadastraHorario ch = new ControladorCadastraHorario();
         listaHorarios = ch.lista(turno);
         int t = listaHorarios.size();
         for (int i = 0; i < t; i++) {
         Horarios horario = listaHorarios.get(i);
         tabelaHorarioTurno.addRow(new Object[]{horario.getInicio(), horario.getFim(), "", "", "", "", "",});

         }*/
    }

    private void listaCurso() {

        ControladorCadastraHorario ch = new ControladorCadastraHorario();
        listaCursos = ch.listaCurso();
        int t = listaCursos.size();
        for (int i = 0; i < t; i++) {
            Curso curso = listaCursos.get(i);
            jComboBoxCurso.addItem(curso.getNome());

        }

    }

    private DefaultTableModel montaTabela() {
        DefaultTableModel tabela = new DefaultTableModel(new String[]{"Início", "Fim", "Segunda", "Terça", "Quarta", "Quinta", "Sexta"}, 0);
        tabela.setNumRows(0);
        int opcao2 = jComboBoxTurno.getSelectedIndex();
        String turno;
        if (opcao2 == 0) {
            turno = "M";
        } else if (opcao2 == 1) {
            turno = "T";
        } else {
            turno = "N";
        }
        ControladorCadastraHorario ch = new ControladorCadastraHorario();
        listaHorarios = ch.lista(turno);
        int tam = listaHorarios.size();

        for (int i = 0; i < tam; i++) {
            Horarios horario = listaHorarios.get(i);
            tabela.addRow(new Object[]{horario.getInicio(), horario.getFim(), "", "", "", "", "",});

        }
        int t = listaAulas.size();
        int y = 0;
        for (int i = 0; i < t; i++) {
            Horarios h = listaAulas.get(i).getHorario();
            int x = h.getCodHorario();
            for (int j = 0; j < listaHorarios.size(); j++) {
                if (listaHorarios.get(j).getCodHorario() != x) {
                } else {
                    y = j;
                    break;
                }
            }

            switch (listaAulas.get(i).getDia().getCodDia()) {
                case "Seg":

                    tabela.setValueAt(listaAulas.get(i).getDisciplina().getNome(), y, 2);
                    break;
                case "Ter":
                    tabela.setValueAt(listaAulas.get(i).getDisciplina().getNome(), y, 3);
                    break;
                case "Qua":
                    tabela.setValueAt(listaAulas.get(i).getDisciplina().getNome(), y, 4);
                    break;
                case "Qui":
                    tabela.setValueAt(listaAulas.get(i).getDisciplina().getNome(), y, 5);
                    break;
                default:
                    tabela.setValueAt(listaAulas.get(i).getDisciplina().getNome(), y, 6);
                    break;
            }

        }

        return tabela;

    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonGerarPdf;
    private javax.swing.JComboBox jComboBoxCurso;
    private javax.swing.JComboBox jComboBoxTurno;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanelBotoes;
    // End of variables declaration//GEN-END:variables
}
